<template>
  <div>
   <div class="goods">
    <div class="menu-wrapper" ref="menuWrapper">
      <ul>
        <li
          v-for="(item,index) in goods"
          :key="index"
          class="menu-item"
          :class="{'current':currenIndex===index}"
          @click="handleClick"
        >
          <span class="text border-bottom">
            <span
              v-show="item.type>0"
              class="icon"
              :class="classMap[item.type]"
            ></span>
            {{item.name}}
          </span>
        </li>
      </ul>
    </div>
    <div class="foods-wrapper" ref="foodsWrapper">
      <ul>
        <li 
        v-for="(item,index) in goods" 
        class="food-list food-list-hook" 
        :key="index" 
        :ref="item.name"
        >
          <h1 class="title">{{item.name}}</h1>
          <ul>
            <li 
            @click="selectFood(food)" 
            v-for="(food,index) in item.foods" 
            :key="index" 
            class="food-item border-bottom"
            >
              <div class="icon">
                <img :src="food.icon" alt="">
              </div>
              <div class="content">
                <h2 class="name">{{food.name}}</h2>
                <p class="desc">{{food.description}}</p>
                <div class="extra">
                  <span class="count">月售{{food.sellCount}}份</span>
                  <span>好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                  <span class="nowPrice">¥{{food.price}}</span>
                  <span class="oldPrice" v-show="food.oldPrice">¥{{food.oldPrice}}</span>
                </div>
                <div class="cartcontrol-wrapper">
                  <cart-control :food="food"  @add="addFood"></cart-control>
                </div>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <shop-cart
      ref="shopcart"
      :select-foods="selectFoods"
      :delivery-price="seller.deliveryPrice"
      :min-price="seller.minPrice"
    ></shop-cart>
  </div>
   <food :food="selectedFood" ref="food" ></food>
  </div>
</template>

<script>
import food from '../../../food/food.vue'
import cartControl from '../../../cartcontrol/cartcontrol.vue'
import shopCart from '../../../shopcart/shopcart.vue'
import Bscroll from 'better-scroll'
export default {
  name: 'TabGoods',
  props: {
    seller: Object,
    goods: Array
  },
  components: {
    shopCart,
    cartControl,
    food
  },
  data () {
    return {
      name: '',
      listHeight: [],
      scrollY: 0,
      selectedFood: {}
    }
  },
  watch: {
    name () {
      if (this.name) {
        const elment = this.$refs[this.name][0]
        this.foodsscroll.scrollToElement(elment, 300)
      }
    }
  },
  computed: {
    currenIndex () {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i]
        let height2 = this.listHeight[i + 1]
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i
        }
      }
      return 0
    },
    selectFoods () {
      let foods = []
      this.goods.forEach((good) => {
        good.foods.forEach((food) => {
          if (food.count) {
            foods.push(food)
          }
        })
      })
      return foods
    }
  },
  created () {
    this.$nextTick(() => {
      this._initScroll()
      this._calculateHeight()
    })
    this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee']
  },
  mounted () {
  },
  methods: {
    handleClick (e) {
      this.name = e.target.innerText
    },
    _initScroll () {
      this.menuscroll = new Bscroll(this.$refs.menuWrapper, {
        click: true
      })
      this.foodsscroll = new Bscroll(this.$refs.foodsWrapper, {
        probeType: 3
      })
      this.foodsscroll.on('scroll', (pos) => {
        this.scrollY = Math.abs(Math.round(pos.y))
      })
    },
    _calculateHeight () {
      let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook')
      let height = 0
      this.listHeight.push(height)
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i]
        height += item.clientHeight
        this.listHeight.push(height)
      }
    },
    selectFood (food) {
      this.selectedFood = food
      this.$refs.food.show()
      console.log(food)
    },
    addFood (target) {
      this._drop(target)
    },
    _drop (target) {
      // 调用shopcar组件中的drop方法，并向其传入当前点击的dom对象
      // 体验优化，异步执行下落动画
      this.$nextTick(() => {
        this.$refs.shopcart.drop(target)
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import "../../../../common/stylus/mixin.styl"
  .goods
    display flex
    position absolute
    overflow hidden
    top 3.48rem
    bottom .92rem
    width 100%
    .menu-wrapper
      flex 0 0 1.6rem
      width 1.6rem
      background: #f3f5f7
      .menu-item
        display table
        padding 0 .24rem
        height 1.08rem
        width 1.12rem
        line-height .28rem
        &.current
          z-index 10
          position relative
          margin-top -0.02rem
          background #ffffff
          font-weight 700
          border none
          .text
            border none
        .icon
          display inline-block
          margin-right .04rem
          width .24rem
          height .24rem
          background-size .24rem .24rem
          background-repeat no-repeat
          &.decrease
            bg-image('decrease_3')
          &.discount
            bg-image('discount_3')
          &.guarantee
            bg-image('guarantee_3')
          &.invoice
            bg-image('invoice_3')
          &.special
            bg-image('special_3')
        .text
          display table-cell
          width 1.12rem
          vertical-align middle
          font-size .24rem
          text-align center
        .border-bottom::before
          background black
          opacity 0.15
    .foods-wrapper
      flex 1
      .title
        padding-left .28rem
        height .52rem
        line-height .52rem
        border-left 2px solid #d9dde1
        font-size .24rem
        color rgb(147,153,159)
        background #f3f5f7
      .food-item
        display flex
        padding  .36rem
        &:last-child
          border none
        .icon
          flex 0 0 1.14rem
          margin-right .2rem
          &>img
            width 100%
            height 100%
        .content
          flex 1
          .name
            margin .04rem 0 .16rem 0
            height .28rem
            line-height .28rem
            font-size .28rem
            color rgb(7,17,27)
          .desc
            margin-bottom .16rem
            line-height .2rem
            font-size .2rem
            color rgb(147,153,159)
          .extra
            margin-bottom .16rem
            line-height .2rem
            font-size .2rem
            color rgb(147,153,159)
            .count
              margin-right .24rem
          .price
            font-weight 700
            line-height .48rem
            .nowPrice
              margin-right .16rem
              font-size .28rem
              color rgb(240,20,20)
            .oldPrice
              text-decoration line-through
              font-size .2rem
              color rgb(147,153,159)
          .cartcontrol-wrapper
            position absolute
            right .20rem
            bottom .20rem
            font-size .24rem
</style>
